//
//  SLPOPDemoController.m
//  Objective-C
//
//  Created by 陆承东 on 2018/7/7.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLPOPDemoController.h"

#import "CustomTransitionContext.h"
#import "Animator.h"

@interface SLPOPDemoController ()

@property (nonatomic, weak) UISegmentedControl *segmentedControl;
@property (nonatomic, weak) UIView *containerView;
@property (nonatomic, strong) UIViewController *currentVC;

@end

@implementation SLPOPDemoController

- (void)initNavBar {
    self.navTitle = @"POP Demo";
    
    NSMutableArray *titleArray = [NSMutableArray new];
    for (int i = 0; i < self.viewControllers.count; i++) {
        UIViewController *vc = self.viewControllers[i];
        [titleArray addObject:vc.title ?: @"未知"];
    }
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
    self.segmentedControl = segmentedControl;
    [segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initNavBar];
    
    [self initView];
    
    self.currentVC = self.viewControllers[0];
}

- (void)initView {
    UIView *containerView = [[UIView alloc] init];
    self.containerView = containerView;
    containerView.backgroundColor = [UIColor clearColor];
    containerView.opaque = true;
    [self.view addSubview:containerView];
    [containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
}

#pragma mark ----- 进行转场动画
- (void)transitionToViewController:(UIViewController *)toVC {
    UIViewController *fromVC = self.currentVC;
    if (toVC == fromVC || ![self isViewLoaded]) {
        return;
    }
    
    UIView *toView = toVC.view;
    toView.frame = self.containerView.bounds;
    [self addChildViewController:toVC];
    
    if (!fromVC) {
        [self.containerView addSubview:toVC.view];
        return;
    }
    
    id<UIViewControllerAnimatedTransitioning> animator = nil;
    if ([self.delegate respondsToSelector:@selector(animatorTransitioningForDemoVC:fromVC:toVC:)]) {
        animator = [self.delegate animatorTransitioningForDemoVC:self fromVC:fromVC toVC:toVC];
    }
    animator = animator ?: [[Animator alloc] init];
    
    // Because of the nature of our view controller, with horizontally arranged buttons,
    // we instantiate our private transition context with information about whether this is a left-to-right or right-to-left transition.
    // The animator can use this information if it wants;
    NSUInteger fromIndex = [self.viewControllers indexOfObject:fromVC];
    NSUInteger toIndex = [self.viewControllers indexOfObject:toVC];
    CustomTransitionContext *transitionContext = [[CustomTransitionContext alloc] initWithFromVC:fromVC toVC:toVC goingRight:toIndex > fromIndex];
    transitionContext.animated = true;
    transitionContext.interactive = false;
    transitionContext.completionBlock = ^(BOOL didComplete) {
        
    };
    [animator animateTransition:transitionContext];
}

#pragma mark ----- Getter和Setter方法
- (void)setCurrentVC:(UIViewController *)currentVC {
    [self transitionToViewController:currentVC];
    _currentVC = currentVC;
    NSInteger index = [self.viewControllers indexOfObject:currentVC];
    if (index != self.segmentedControl.selectedSegmentIndex) {
        [self.segmentedControl setSelectedSegmentIndex:index];
    }
}

#pragma mark ----- 点击事件
- (void)segmentedControlValueChanged:(UISegmentedControl *)sender {
    SLog(@"segmentedControlValueChanged - selectedSegmentIndex: %ld", sender.selectedSegmentIndex);
    self.currentVC = self.viewControllers[sender.selectedSegmentIndex];
    
}

@end

//@implementation PrivateAnimatedTransition
//
//static CGFloat const kChildViewPadding = 16.0;
//static CGFloat const kDamping = 0.75;
//static CGFloat const kInitialSpringVelocity = 0.5;
//
//- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
//    return 0.5;
//}
//
//- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
////    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
////    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//
//}
//
//@end
